import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart';
import '../../app_files.dart';
import 'action.dart';
import 'state.dart';
import 'dart:io';

Widget buildView(
    UserCenterState state, Dispatch dispatch, ViewService viewService) {
  double rpx =
      double.tryParse((ScreenUtil.screenWidthDp / 375).toStringAsFixed(2));

  Widget _gasUser() {
    final List groupOne = [
      {'name': '我的资料', 'icon': 'user_info.png'},
      {'name': '我的账号', 'icon': 'user_account.png'},
      {'name': '燃气户号管理', 'icon': 'user_gasNo.png'},
    ];

    Widget _groupOne() {
      List<Widget> widgets = groupOne.map<Widget>((e) {
        int index = groupOne.indexOf(e);
        return InkWell(
          child: Container(
            height: index == groupOne.length - 1 ? 60 : 51,
            padding: EdgeInsets.only(left: 15, right: 15),
            decoration: BoxDecoration(
              color: Colors.white,
              border: Border(
                bottom: BorderSide(
                  color: ThemeConfig.separeColor,
                  width: index == groupOne.length - 1 ? 10 : 1,
                ),
              ),
            ),
            child: Row(
              children: <Widget>[
                Image.asset('images/${e['icon']}'),
                Padding(
                  padding: EdgeInsets.only(left: 12),
                  child: Text(
                    e['name'],
                    style: TextThemeConfig.UserCenterItemStyle,
                  ),
                ),
                Expanded(
                  child: Container(),
                ),
                Padding(
                    padding: EdgeInsets.only(right: 12),
                    child: Image.asset('images/right_arrow.png')),
              ],
            ),
          ),
          onTap: () => dispatch(UserCenterActionCreator.onGasTap(e['name'])),
        );
      }).toList();
      return Column(
        children: widgets,
      );
    }

    final List groupTwo = [
      {'name': '设置', 'icon': 'user_setting.png'},
      {'name': '意见反馈', 'icon': 'user_feedback.png'},
      {'name': '客服电话', 'icon': 'user_phone.png'},
    ];

    Widget _groupTwo() {
      List<Widget> widgets = groupTwo.map<Widget>((e) {
        int index = groupTwo.indexOf(e);
        return InkWell(
          child: Container(
            height: index == groupTwo.length - 1 ? 50 : 51,
            padding: EdgeInsets.only(left: 15, right: 15),
            decoration: BoxDecoration(
              color: Colors.white,
              border: Border(
                bottom: BorderSide(
                  color: ThemeConfig.separeColor,
                  width: index == groupTwo.length - 1 ? 0 : 1,
                ),
              ),
            ),
            child: Row(
              children: <Widget>[
                Image.asset('images/${e['icon']}'),
                Padding(
                  padding: EdgeInsets.only(left: 12),
                  child: Text(
                    e['name'],
                    style: TextThemeConfig.UserCenterItemStyle,
                  ),
                ),
                Expanded(
                  child: Container(),
                ),
                Padding(
                    padding: EdgeInsets.only(right: 12),
                    child: Image.asset('images/right_arrow.png')),
              ],
            ),
          ),
          onTap: () => dispatch(UserCenterActionCreator.onGasTap(e['name'])),
        );
      }).toList();
      return Column(
        children: widgets,
      );
    }

    return Scaffold(
        backgroundColor: Colors.white,
        body: Column(
          children: [
            Stack(
              children: [
                Container(
                  width: ScreenUtil.screenWidthDp,
                  height: 180 * rpx,
                  child: Image.asset(
                    'images/user_bg.png',
                    width: ScreenUtil.screenWidthDp,
                    fit: BoxFit.fitWidth,
                  ),
                ),
                Positioned(
                  child: Container(
                    child: Center(
                      child: Text(
                        '个人中心',
                        style: TextThemeConfig.Font18WithColorWhite,
                      ),
                    ),
                  ),
                  top: 17 * rpx + ScreenUtil.statusBarHeight,
                  left: 0,
                  right: 0,
                ),
                Positioned(
                  child: Container(
                    padding: EdgeInsets.only(left: 15),
                    child: Row(
                      children: [
                        Container(
                          width: 66,
                          height: 66,
                          decoration: BoxDecoration(
                              borderRadius:
                                  BorderRadius.all(Radius.circular(33)),
                              border:
                                  Border.all(color: Colors.white, width: 1.5)),
                          child: ClipRRect(
                            borderRadius:
                                BorderRadius.all(Radius.circular(32.25)),
                            child: CustomImage(
                              state.userInfo.headimg,
                              width: 64.5,
                              height: 64.5,
                              placeholderUrl: 'images/default_head.png',
                            ),
                          ),
                        ),
                        Padding(padding: EdgeInsets.only(left: 20)),
                        Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Row(
                              children: [
                                Text(
                                  state.userInfo.nickname.length == 0
                                      ? '燃气用户'
                                      : state.userInfo.nickname,
                                  style: TextThemeConfig.Font18WithColorWhite,
                                ),
                                Padding(padding: EdgeInsets.only(left: 10)),
                                state.userInfo.gender == null
                                    ? Container()
                                    : state.userInfo.gender
                                        ? Image.asset('images/icon_girl.png')
                                        : Image.asset('images/icon_boy.png')
                              ],
                            ),
                            Padding(
                              padding: EdgeInsets.only(top: 14),
                              child: Text(
                                state.userInfo.mobile,
                                style: TextThemeConfig.Font16WithWhite,
                              ),
                            ),
                          ],
                        ),
                      ],
                    ),
                  ),
                  top: 58 * rpx + ScreenUtil.statusBarHeight,
                ),
                Positioned(
                  child: Container(
                    height: 10,
                    width: ScreenUtil.screenWidthDp,
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.only(
                          topLeft: Radius.circular(19),
                          topRight: Radius.circular(19)),
                      color: Colors.white,
                    ),
                  ),
                  bottom: 0,
                ),
              ],
            ),
            SingleChildScrollView(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  _groupOne(),
                  _groupTwo(),
                  Container(
                    height: 60 * rpx,
                  ),
                  Padding(
                    padding: EdgeInsets.only(left: 15, right: 15),
                    child: InkWell(
                      child: Container(
                        height: 50,
                        decoration: BoxDecoration(
                          color: ThemeConfig.mainColor,
                          borderRadius: BorderRadius.all(Radius.circular(4)),
                        ),
                        child: Center(
                          child: Text(
                            '退出登录',
                            style: TextThemeConfig.Font16WithWhite,
                          ),
                        ),
                      ),
                      onTap: () => dispatch(UserCenterActionCreator.onLogout()),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ));
  }

  if (state.userInfo.isGsUser) return _gasUser();
  return Scaffold(
    resizeToAvoidBottomPadding: false,
    body: SingleChildScrollView(
      child: Column(
        children: [
          Container(
            height: ScreenUtil.statusBarHeight,
            color: ThemeConfig.mainColor,
          ),
          Container(
            color: Colors.white,
            child: Stack(
              children: [
                Image.asset(
                  'images/center_bg.png',
                  width: double.infinity,
                  fit: BoxFit.fitWidth,
                ),
                Positioned(
                  child: Align(
                    child: ClipRRect(
                      borderRadius: BorderRadius.all(Radius.circular(34)),
                      child: CustomImage(
                        state.userInfo.headimg,
                        width: 68,
                        height: 68,
                        fit: BoxFit.fill,
                        placeholderUrl: 'images/default_head.png',
                      ),
                    ),
                  ),
                  bottom: 13 * rpx,
                  left: 0,
                  right: 0,
                ),
                Positioned(
                  child: Align(
                    child: Text(
                      '我的',
                      style: TextThemeConfig.AppBarTitle,
                    ),
                  ),
                  top: 6,
                  left: 0,
                  right: 0,
                ),
                Positioned(
                  top: 0,
                  right: 12,
                  child: Container(
                    width: 40,
                    height: Platform.isAndroid ? 40 : 36,
                    child: Align(
                      child: GestureDetector(
                        child: Image.asset(
                          'images/setting.png',
                        ),
                        behavior: HitTestBehavior.translucent,
                        onTap: () => CommonUtils.pushRoute(
                            viewService.context, RoutePath.settingView),
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
          Container(
            color: Colors.white,
            child: Column(
              children: [
                Text(
                  state.userInfo.nickname,
                  style: TextThemeConfig.Font17WithColor22,
                ),
                Row(
                  children: [
                    Expanded(
                      child: Container(
                        height: 85,
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Text(
                              '部门',
                              style: TextThemeConfig.Font14WithColor99,
                            ),
                            Padding(padding: EdgeInsets.only(top: 15)),
                            Text(
                              state.userInfo.department,
                              style: TextThemeConfig.Font18WithColor22,
                            ),
                          ],
                        ),
                      ),
                    ),
                    Container(
                      width: 0.5,
                      height: 40,
                      color: Color(0xffc2c2c2),
                    ),
                    Expanded(
                      child: Container(
                        height: 85,
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Text(
                              '当前角色',
                              style: TextThemeConfig.Font14WithColor99,
                            ),
                            Padding(padding: EdgeInsets.only(top: 15)),
                            Text(
                              state.userInfo.currentRoleName,
                              style: TextThemeConfig.Font18WithColor22,
                            ),
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
          Container(
            color: Colors.white,
            margin: EdgeInsets.only(top: 12),
            padding: EdgeInsets.only(left: 15, right: 15),
            child: Column(
              children: [
                InkWell(
                  onTap: () => dispatch(UserCenterActionCreator.onTap(0)),
                  child: Container(
                    height: 50,
                    child: Row(
                      children: [
                        Image.asset('images/icon_info.png'),
                        Expanded(
                            child: Container(
                          padding: EdgeInsets.only(left: 8),
                          child: Text(
                            '我的资料',
                            style: TextThemeConfig.Font15WithColor22,
                          ),
                        )),
                        Image.asset('images/right_arrow.png'),
                      ],
                    ),
                    decoration: BoxDecoration(
                      border: Border(
                          bottom: BorderSide(color: ThemeConfig.separeColor)),
                    ),
                  ),
                ),
                InkWell(
                  onTap: () => dispatch(UserCenterActionCreator.onTap(1)),
                  child: Container(
                    height: 50,
                    child: Row(
                      children: [
                        Image.asset('images/icon_account.png'),
                        Expanded(
                            child: Container(
                          padding: EdgeInsets.only(left: 8),
                          child: Text(
                            '我的账号',
                            style: TextThemeConfig.Font15WithColor22,
                          ),
                        )),
                        Image.asset('images/right_arrow.png'),
                      ],
                    ),
                  ),
                ),
                state.userInfo.roleNames != null &&  state.userInfo.roleNames.length == 1
                    ? Container()
                    : InkWell(
                        onTap: () => dispatch(UserCenterActionCreator.onTap(2)),
                        child: Container(
                          height: 50,
                          child: Row(
                            children: [
                              Image.asset('images/icon_juese.png'),
                              Expanded(
                                  child: Container(
                                padding: EdgeInsets.only(left: 8),
                                child: Text(
                                  '工作身份',
                                  style: TextThemeConfig.Font15WithColor22,
                                ),
                              )),
                              Image.asset('images/right_arrow.png'),
                            ],
                          ),
                        ),
                      ),

                state.url.isEmpty
                    ? Container()
                    : InkWell(
                  onTap: () => CommonUtils.pushWebViewFlutterPage(viewService.context, '操作说明', state.url),
                  child: Container(
                    height: 50,
                    child: Row(
                      children: [
                        Image.asset('images/user_setting.png'),
                        Expanded(
                            child: Container(
                              padding: EdgeInsets.only(left: 8),
                              child: Text(
                                '操作说明',
                                style: TextThemeConfig.Font15WithColor22,
                              ),
                            )),
                        Image.asset('images/right_arrow.png'),
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ),
          Container(
            height: 60 * rpx,
          ),
          Padding(
            padding: EdgeInsets.only(left: 15, right: 15),
            child: InkWell(
              child: Container(
                height: 50,
                decoration: BoxDecoration(
                  color: ThemeConfig.mainColor,
                  borderRadius: BorderRadius.all(Radius.circular(4)),
                ),
                child: Center(
                  child: Text(
                    '退出登录',
                    style: TextThemeConfig.Font16WithWhite,
                  ),
                ),
              ),
              onTap: () => dispatch(UserCenterActionCreator.onLogout()),
            ),
          ),
        ],
      ),
    ),
  );
}
